<template>
<div style="height:750px" >
  <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
        <!-- <el-input style="position:fixed;bottom:-9999px;"></el-input> -->
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <!-- <el-input style="position:fixed; top:-9999px;" type="password"></el-input> -->
        <el-input v-model="ruleForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="ruleForm.roles.id" placeholder="请选择用户类型">
          <el-option label="工人" :value="2"></el-option>
          <el-option label="客户" :value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="用户性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex" size="medium">
          <el-radio border label="男"></el-radio>
          <el-radio border label="女"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="电话" prop="tel">
        <el-input v-model="ruleForm.tel" @keyup.enter.native="save('ruleForm')"></el-input>
      </el-form-item>
    </el-form>


          <el-form-item label="地址" prop="addr">
          <el-input v-model="ruleForm.addr" @keyup.enter.native="save('ruleForm')"></el-input>
        </el-form-item>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="save('ruleForm')">确 定</el-button>
      <el-button @click="resetForm('ruleForm')">取 消</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
    import { postRequest} from "../../api/api";
export default {
  data() {
    return {
      dialogFormVisible: true,
      ruleForm: {
        username: "",
        password: "",
        age: "",
        sex: "男",
        tel: "",
        roles: { id: 2 },
        userstate:0,
        addr:""
      },
      rules: {
        username: [{ required: true, message: "请输入名字", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    save(formName) {
      console.log(this.ruleForm.roles.id);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postRequest("user/register",this.ruleForm
          )
            .then((res) => {
              if (res.data.code === 200) {
                this.dialogFormVisible = false;
                this.$refs[formName].resetFields();
                this.$message.success("添加用户成功");
                if (this.ruleForm.roles.id == 2) {
                  this.$router.push({
                    path: "/index1",
                  });
                } else {
                  this.$router.push({
                    path: "/index2",
                  });
                }
              } else {
                this.$message.error("添加用户失败");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
      this.$router.push({
        path: "/index1",
      });
    },
  },
};
</script>

<style></style>
